clear
set more off

* -----------------------------------------------------------
use "${final}/oisl_clean_data.dta", clear

 summ age, d
gen age_median = (age >= r(p50))

 summ savings, d
gen savings_median = (savings >= r(p50))



destring id, replace
xtset id month

gen num_mob_lastmonth = l.num_mob
replace num_mob_lastmonth = 0 if num_mob_lastmonth == .

global het female age_median accra savings_median 

local h = 1
foreach het in $het{
	gen treatment_2_`het'=treatment_2*`het'
	gen treatment_3_`het'=treatment_3*`het'
	
	est clear
	foreach var in any_mob sum_mob num_mob  {
		eststo: xtreg `var' treatment_2 treatment_3 /*
		*/					treatment_2_`het' treatment_3_`het'/*
		*/					`var'_base `het' i.strata i.month , /*
		*/					  vce(cluster id)
		estadd local Base 	"No"
		estadd local SE 	"Yes"
		estadd local WE 	"Yes"
		quietly summ `var' if (treatment == 0 & `het' == 0 & /*
		*/					   month == 8)
		estadd scalar mean0=r(mean)
		quietly summ `var' if (treatment == 0 & `het' == 1 & /*
		*/					   month == 8)
		estadd scalar mean1=r(mean)
		quietly test treatment_2 = treatment_3
		estadd scalar test = r(p)
		lincom treatment_3+treatment_3_`het' 
		estadd scalar total = r(estimate)
		estadd scalar total_p = r(p)
		
			
	}
	
		eststo: xtreg savings treatment_2 treatment_3 /*
		*/					treatment_2_`het' treatment_3_`het'/*
		*/					savings_base `het' i.strata i.month, /*
		*/					  vce(cluster id)
		estadd local WE 	"Yes"
		quietly summ savings if (treatment == 0 & `het' == 0 & /*
		*/					   month == 8)
		estadd scalar mean0=r(mean)
		quietly summ savings if (treatment == 0 & `het' == 1 & /*
		*/					   month == 8)
		estadd scalar mean1=r(mean)
		quietly test treatment_2 = treatment_3
		estadd scalar test = r(p)
		lincom treatment_3+treatment_3_`het' 
		estadd scalar total = r(estimate)
		estadd scalar total_p = r(p)


	
		eststo: xtreg late_repayment treatment_2 treatment_3 /*
		*/					treatment_2_`het' treatment_3_`het'/*
		*/					late_repayment_base `het' i.strata i.month , /*
		*/					  vce(cluster id)
		estadd local WE 	"Yes"
		quietly summ late_repayment if (treatment == 0 & `het' == 0 & /*
		*/					   strata == 3 & month == 8)
		estadd scalar mean0=r(mean)
		quietly summ late_repayment if (treatment == 0 & `het' == 1 & /*
		*/					   month == 8 & strata==3) 
		estadd scalar mean1=r(mean)
		quietly test treatment_2 = treatment_3
		estadd scalar test = r(p)		
		lincom treatment_3+treatment_3_`het' 
		estadd scalar total = r(estimate)
		estadd scalar total_p = r(p)

	
	esttab using "${output}/hetero_`het'.tex", replace /*
	*/	   sfmt(%12.3fc) number booktabs f b(3) se(3) label depvars/*
	*/	   star(* 0.1 ** 0.05 *** 0.01)							/*
	*/	   keep(treatment_2 treatment_3  /*
	*/	   		treatment_2_`het' treatment_3_`het') /*
	*/	   order(treatment_2 treatment_3 treatment_2_`het' /*
	*/			 treatment_3_`het' )/*	
	*/	   coeflabel(treatment_2 		"Saving " /*
	*/				 treatment_3 		"Mobile Banking "/*
	*/				 treatment_2_`het' "Saving  $\times$ ..."/*
	*/	 			 treatment_3_`het' "MB  $\times$ ..." /*
	*/				  			"...")/*
	*/	   mtitles("Any MoB" "Sum MoB" "No. MoB" "Savings" /*
	*/			   "Late Repayment") /*
	*/     collabels(none) compress /*
	*/	   scalars("N Observations" "mean0 Control Mean `het'=0" "mean1 Control Mean `het'=1" /*
	*/			   "test p-Value Savings  = MB" "total Total effect MB" /*
	*/ "total_p Total Effect MB p-value"  /*
	*/			   "WE Wave FE") 
	local h = `h' + 1
}


// For loan
gen treatment_2_loan=treatment_2*loan
gen treatment_3_loan=treatment_3*loan

est clear
foreach var in any_mob sum_mob num_mob {
	eststo: xtreg `var' treatment_2 treatment_3 /*
	*/					treatment_2_loan treatment_3_loan/*
	*/					`var'_base loan i.strata i.month $controls, /*
	*/					  vce(cluster id)
	estadd local WE 	"Yes"
	quietly summ `var' if (treatment == 0 & loan == 0 & /*
	*/					   month == 8)
		estadd scalar mean0=r(mean)
		quietly summ `var' if (treatment == 0 & loan == 1 & /*
		*/					   month == 8)
		estadd scalar mean1=r(mean)
	quietly test treatment_2 = treatment_3
	estadd scalar test = r(p)
	lincom treatment_3+treatment_3_loan
	estadd scalar total = r(estimate)
	estadd scalar total_p = r(p)

		
}

	eststo: xtreg savings treatment_2 treatment_3 /*
	*/					treatment_2_loan treatment_3_loan/*
	*/					savings_base loan i.strata i.month $controls, /*
	*/					  vce(cluster id)
	estadd local WE 	"Yes"
	quietly summ savings if (treatment == 0 & loan == 0 & /*
	*/					   month == 8)
		estadd scalar mean0=r(mean)
		quietly summ savings if (treatment == 0 & loan == 1 & /*
		*/					   month == 8)
		estadd scalar mean1=r(mean)
	quietly test treatment_2 = treatment_3
	estadd scalar test = r(p)
	lincom treatment_3+treatment_3_loan
	estadd scalar total = r(estimate)
	estadd scalar total_p = r(p)



	eststo: xtreg late_repayment treatment_2 treatment_3 /*
	*/					late_repayment_base i.strata i.month, /*
	*/					  vce(cluster id)
	estadd local WE 	"Yes"
	quietly summ late_repayment if (treatment == 0  & loan == 0 & /*
	*/					   strata == 3 & month == 8)
		estadd scalar mean0=r(mean)
		quietly summ late_repayment if (treatment == 0 & loan == 1 & /*
		*/					   month == 8 & strata==3)
		estadd scalar mean1=r(mean)
	quietly test treatment_2 = treatment_3
	estadd scalar test = r(p)
	


esttab using "${output}/hetero_loan.tex", replace /*
*/	   sfmt(%12.3fc) number booktabs f b(3) se(3) label depvars/*
*/	   star(* 0.1 ** 0.05 *** 0.01)							/*
*/	   keep(treatment_2 treatment_3  /*
*/	   		treatment_2_loan treatment_3_loan) /*
*/	   order(treatment_2 treatment_3 treatment_2_loan /*
*/			 treatment_3_loan )/*	
*/	   coeflabel(treatment_2 		"Savings " /*
*/				 treatment_3 		"Mobile Banking "/*
*/				 treatment_2_loan "Savings  $\times$ Loan"/*
*/	 			 treatment_3_loan "MB  $\times$ Loan" )/*
*/	   mtitles("Any MoB" "Sum MoB" "No. MoB" "Savings" /*
*/			   "Late Repayment") /*
*/     collabels(none) compress /*
*/	   scalars("N Observations" "mean0 Control Mean loan=0" "mean1 Control Mean loan=1" /*
*/			   "test p-value Savings  = MB" "total Total effect MB" /*
*/ "total_p Total Effect MB p-value"  /*
*/			    "WE Wave FE") 


